package com.nuoniu.sibanyun.controller.production;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.dto.BOMDetailDto;
import com.nuoniu.sibanyun.entity.dto.ChangeWorkerDto;
import com.nuoniu.sibanyun.entity.dto.VProductionWorkDetailSearchDto;
import com.nuoniu.sibanyun.entity.dto.mobile.ProcedureDetailSearchDto;
import com.nuoniu.sibanyun.entity.production.VProductionDispatchMain;
import com.nuoniu.sibanyun.entity.production.VProductionWorkInventoryDetail;
import com.nuoniu.sibanyun.entity.vo.VProcedureDetailSimpleVo;
import com.nuoniu.sibanyun.entity.vo.VProductionDispatchMainVo;
import com.nuoniu.sibanyun.entity.vo.VProductionWorkProcedureDetailDispatchVo;
import com.nuoniu.sibanyun.entity.vo.reportVo.UnFinishProductionWorkMainVo;
import com.nuoniu.sibanyun.service.production.IVProductionDispatchMainService;
import com.nuoniu.sibanyun.service.production.IVProductionWorkProcedureDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.nuoniu.sibanyun.common.BaseController;

import java.util.List;

/**
 * <p>
 * 生产制造——派工单 前端控制器
 * </p>
 *
 * @author 小啊刚
 * @since 2021-04-19
 */
@RestController
@RequestMapping("/production/dispatch")
@Api(value = "派工单", tags = "生产制造_派工单")
public class VProductionDispatchMainController extends BaseController {

    @Autowired
    private IVProductionDispatchMainService productionDispatchMainService;

    @Autowired
    private IVProductionWorkProcedureDetailService procedureDetailService;

    @ApiOperation("获取单据编码")
    @GetMapping("getCode")
    public HttpResult getCode() {
        return HttpResult.ok("",productionDispatchMainService.getCode());
    }

    @ApiOperation("保存")
    @PostMapping("save")
    public HttpResult save(@RequestBody VProductionDispatchMain productionDispatchMain) {
        productionDispatchMainService.saveDispatch(productionDispatchMain);
        return HttpResult.ok();
    }

    @GetMapping("examine")
    @ApiOperation("单据审核")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "单据id"),
            @ApiImplicitParam(name = "status", value = "状态 0:审核,1:已审")
    })
    public HttpResult examine(Long id, Integer status) {
        if (null == status || status < 0 || status > 1)
            throw new NuoNiuException("状态参数错误");
        productionDispatchMainService.examine(id,status);
        return HttpResult.ok();
    }

    @ApiOperation("删除单据")
    @DeleteMapping("delete")
    public HttpResult delete(Long id) {
        productionDispatchMainService.deleteById(id);
        return HttpResult.ok();
    }


    @GetMapping("/getOneByCondition")
    @ApiOperation(value = "获取 派工单（上一页、下一页、首页 和 尾页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "actionType", value = "动作 1：上一页 2：下一页 3：首页 4：尾页"),
            @ApiImplicitParam(name = "currentId",  value = "当前记录ID,没有就不传")
    })
    public HttpResult<VProductionDispatchMainVo> getOneByCondition(Integer actionType, Long currentId) {
        return HttpResult.ok(productionDispatchMainService.getOneByCondition(actionType, currentId));
    }

    @ApiOperation("根据bomID获取尾阶 物料需求量")
    @GetMapping("listLeafDetailByMother")
    public HttpResult<List<VProductionWorkInventoryDetail>> listLeafDetailByMother(BOMDetailDto bomDetailDto) {
        return HttpResult.ok(productionDispatchMainService.listLeafDetailByMother(bomDetailDto));
    }

    @ApiOperation("获取某个产品明细 已经派遣的最大次数")
    @GetMapping("getMaxDispatchTimes")
    public HttpResult<List<VProductionWorkInventoryDetail>> listLeafDetailByMother(Long workDetailId) {
        return HttpResult.ok(productionDispatchMainService.getMaxDispatchTimes(workDetailId));
    }



    @ApiOperation("根据数据权限 获取未完工 的派工任务(废弃)")
    @PostMapping("listUnFinishProcedureDetailDispatchVo")
    public HttpResult<VProductionWorkProcedureDetailDispatchVo> listUnFinishProcedureDetailDispatchVo(@RequestBody VProductionWorkDetailSearchDto productionDispatchMainSearchDto) {
        return HttpResult.ok(procedureDetailService.getUnFinishProcedureDetailDispatchVo(productionDispatchMainSearchDto));
    }


    @ApiOperation("根据数据权限 查询 未完成产品明细 和 工序明细")
    @PostMapping("getUnFinishProductionWorkVo")
    public HttpResult<UnFinishProductionWorkMainVo> getUnFinishProductionWorkVo(@RequestBody VProductionWorkDetailSearchDto productionDispatchMainSearchDto) {
        return HttpResult.ok(procedureDetailService.getUnFinishProductionWorkVo(productionDispatchMainSearchDto));
    }


    @ApiOperation("工序派工任务 变更工人")
    @PostMapping("changeWorkId")
    public HttpResult changeWorkId(@RequestBody ChangeWorkerDto changeWorkerDto) {
        procedureDetailService.changeWorkId(changeWorkerDto);
        return HttpResult.ok();
    }

    @ApiOperation(value = "获取未完成的派工工序任务单（移动端）",tags = "APP_生产制造")
    @GetMapping("pageVProcedureDetailSimpleVo")
    public HttpResult<VProcedureDetailSimpleVo> pageVProcedureDetailSimpleVo(Page page, ProcedureDetailSearchDto procedureDetailSearchDto) {
        return HttpResult.ok(procedureDetailService.pageVProcedureDetailSimpleVo(page, procedureDetailSearchDto));
    }


}
